﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace MvcWeb.Controllers
{
    public class ModuleController : BaseController
    {
        // GET: /Module/
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Edit(int? id, EAM.Model.Sys_Module viewModel)
        {
            if (id == null)
            {
                viewModel.ParentId = viewModel.ParentId ?? 0;
                db.Sys_Modules.Add(viewModel);
            }
            else
            {
                var model = db.Sys_Modules.Find(id);
                if (model != null)
                {
                    db.Sys_Modules.Attach(model);
                    model.Name = viewModel.Name;
                    model.Url = viewModel.Url;
                    model.OrderIndex = viewModel.OrderIndex;
                }
            }

            bool flag = true;
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                flag = false;
            }

            return Json(flag);
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            var model = db.Sys_Modules.Find(id);
            db.Sys_Modules.Remove(model);
            db.SaveChanges();

            return Json(true);
        }

        public ActionResult Root()
        {
            var allNodeList = db.Sys_Modules.ToList();
            List<ModuleNode> moduleList = new List<ModuleNode>();
            ModuleNode node = new ModuleNode { id = 0, text = "根节点" };
            moduleList.Add(node); //虽然奇怪，但是ligerui认不出不带[]的json，所以只好多加到list
            SetTreeNode(allNodeList, node);

            return Json(moduleList, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Nav()
        {
            List<EAM.Model.Sys_Module> nodeList = null;
            if (User.Identity.Name.ToLower() == "admin")
            {
                nodeList = db.Sys_Modules.ToList();
            }
            else
            {
                var userRoles = Roles.GetRolesForUser();
                var roleIdList = db.aspnet_Roles.Where(m => userRoles.Contains(m.RoleName)).Select(m => m.RoleId).ToList();
                var privilegeList = db.Role_Priviledges.Where(m => roleIdList.Contains(m.RoleId))
                    .Select(m => m.Priviledge).AsEnumerable()
                    .Select(m => int.Parse(m)).ToList();

                nodeList = db.Sys_Modules.Where(m => privilegeList.Contains(m.Id)).ToList();
            }
            
            ModuleNode node = new ModuleNode { id = 0, text = "根节点" };
            SetTreeNode(nodeList, node);

            return Json(node.children, JsonRequestBehavior.AllowGet);
        }

        [NonAction]
        void SetTreeNode(List<EAM.Model.Sys_Module> allNodeList, ModuleNode node)
        {
            var list = allNodeList.Where(m => m.ParentId == node.id).OrderBy(m => m.OrderIndex).ToList();
            if (list.Count > 0)
            {
                node.children = new List<ModuleNode>();
                ModuleNode child = null;
                foreach (var m in list)
                {
                    child = new ModuleNode { id = m.Id, text = m.Name, url = m.Url, pid = m.ParentId ?? 0, oid = m.OrderIndex ?? 0 };
                    node.children.Add(child);
                    SetTreeNode(allNodeList, child);
                }
            }
        }
    }

    //模块列表，嵌套
    public class ModuleNode
    {
        public int id { get; set; }
        public string text { get; set; }
        public string url { get; set; }
        public int pid { get; set; }
        public int oid { get; set; }
        public List<ModuleNode> children { get; set; }
    }
}
